package screens;

import javax.swing.table.TableStringConverter;

import media.AudioManager;
import media.Musics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.me.jnrgame.JnR;

public class MainMenuScreen implements Screen {

	private JnR game;
	private Stage stage;
	private LevelScreen levelScreen;
	private Skin skin;

	public MainMenuScreen(JnR _game) {
		super();
		game = _game;
		game.setMainMenuScreen(this);
		stage = new Stage(game.getScreenWidth(), game.getScreenHeight(), true);
		TextureAtlas menuAtlas = new TextureAtlas(
				Gdx.files.internal("data/screen-layouts/mainmenu/uiskin.atlas"));
		Gdx.input.setInputProcessor(stage);
		skin = new Skin(
				Gdx.files.internal("data/screen-layouts/mainmenu/uiskin.json"),
				menuAtlas);
		Table table = new Table(skin);
		table.setWidth(stage.getWidth());
		table.setHeight(stage.getHeight());
		table.add("AlienRush").spaceBottom(50);
		table.row();

		TextButton startGameButton = new TextButton("Start game", skin);
		startGameButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
					if (game.getLevelScreen() != null && !game.getLevelScreen().getWorldAssitant().getPlayer().isGameOver()) {
					game.toStartGameScreen();
				} else {
					levelScreen = new LevelScreen(2, game);
					game.setUpLevel(levelScreen);
					AudioManager.playMusic(Musics.Level);
				}
		
				System.out.println("start game");
			}

		});
		table.add(startGameButton).size(300f, 60f).uniform().spaceBottom(10);
		table.row();

		TextButton hallOfFameButton = new TextButton("Hall of Fame", skin);
		hallOfFameButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
					stage.addActor(new HallOfFameWindow(skin, game, stage, 0,0));
			}

		});
		table.add(hallOfFameButton).size(300f, 60f).uniform().spaceBottom(10);
		table.row();
		
		// register the button "options"
		TextButton optionsButton = new TextButton("Options", skin);
		optionsButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				game.toOptionsScreen(0);
				Gdx.input.setInputProcessor(game.getOptionsScreen().getStage());
				System.out.println("options");
			}
		});
		table.add(optionsButton).size(300f, 60f).uniform().spaceBottom(10);
		table.row();

		TextButton exitGameButton = new TextButton("Exit Game", skin);
		exitGameButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				game.dispose();
			}

		});
		table.add(exitGameButton).size(300f, 60f).uniform().spaceBottom(10);
		stage.addActor(table);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
		// Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	public Stage getStage() {

		return stage;
	}
}
